/**
 * 
 */

function dispDeptView() {

	$("#b1txt").text("View Open Complaints");
	$("#b2txt").text("View Resolved Complaints");
	$("#b3txt").text("View Reports");
	$("#b4txt").text("View Notifications");
	
	
	$("#SideBarButton4").slideDown();

	$("#SideBarButton1").off().click(function() {
		showOpenComplaints();
	});
	$("#SideBarButton2").off().click(function() {
		showAllComplaints();
	});
	$("#SideBarButton3").off().click(function() {
		dispDeptCompPieChart();
	});
	$("#SideBarButton4").off().click(function() {
		dispdeptNotification();
	});

	$("#dispCont .grid").hide();

	showOpenComplaints();
}

function showOpenComplaints() {

	$("#dispCont .grid").hide();

	$("#SideBarButton1").removeClass("info").addClass("warning");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	$("#complaintsTable tbody").empty();

	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/getComplaints",
		data : {
			field : "status",
			key : "open"
		},
		success : function(data) {
			// console.log(data);
			$.each(data, function(key, val) {
				var row = "<tr><td>" + val.cid + "</td><td>" + val.location + "</td><td>" + val.date_submission + "</td><td>"
						+ val.description + "</td><td><button class="
						+ "deptResolve" + ">Resolve</button></td></tr>";
				$("#complaintsTable").append(row);
			});
//</td><td><button class=" + "deptEdit" + ">Edit</button>
			//edit button
/*			$(".deptEdit").click(function() {
				var row = $(this).parents('tr').get(0);

				var status = editComplaint($(row.cells[0]).text().trim());
				if (status = "true") {
					console.log(this);
					$(this).removeClass("warning").addClass("success");
					$(this).off();
					$(this).text("Resolved");
				} else if (status == "false") {
					$(this).addClass("warning");
				}
			});*/
			
			//resolve button
			$(".deptResolve").click(function() {
				var row = $(this).parents('tr').get(0);

				var status = resolveComplaint($(row.cells[0]).text().trim());
				if (status = "true") {
					console.log(this);
					$(this).removeClass("warning").addClass("success");
					$(this).off();
					$(this).text("Resolved");
				} else if (status == "false") {
					$(this).addClass("warning");
				}
			});

			//search
			var $rows = $('#complaintsTable tr').not('thead tr');
			$('#InputCompTableSearch').keyup(function() {
				var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

				$rows.show().filter(function() {
					var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
					return !~text.indexOf(val);
				}).hide();
			});

		}
	});

	$("#ComplaintList").slideDown();
}

function dispDeptCompPieChart() {

	$("#dispCont .grid").hide();
	$("#divChartContainer").slideDown();

	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("info").addClass("warning");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	var reportData = [];

	$.getJSON("/ComplaintReg/rest/service/getReportByDept", function(data) {
		$.each(data, function(key, value) {
			reportData.push(value);
		});

		var chart = AmCharts.makeChart("chartdiv", {
			"type" : "pie",
			"theme" : "none",
			"dataProvider" : reportData,
			"valueField" : "value",
			"titleField" : "Label",
			"radius" : "30%",
			"innerRadius" : "50%",
			"exportConfig" : {
				menuItems : [ {
					icon : '/lib/3/images/export.png',
					format : 'png'
				} ]
			}
		});
	});
}

function resolveComplaint(cid) {
	console.log("resolving : " + cid);

	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/resolveComplaint",
		data : {
			'cid' : cid
		},
		success : function(data) {
			return data;
		}
	});
}


//manish.................................
function showAllComplaints() {

	$("#dispCont .grid").hide();

	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("info").addClass("warning");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	$("#complaintsTable tbody").empty();

	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/getComplaints",
		data : {
			field : "status",
			key : "resolved" 
		},
		success : function(data) {
			// console.log(data);
			$.each(data, function(key, val) {
				var row = "<tr><td>" + val.cid + "</td><td>" + val.location + "</td><td>" + val.date_submission + "</td><td>"
						+ val.description + "</td><td><button class="
						+ "deptOpened" + ">Open</button></td></tr>";
				$("#complaintsTable").append(row);
			});
			$(".deptOpened").click(function() {
				var row = $(this).parents('tr').get(0);

				var status = openComplaint($(row.cells[0]).text().trim());
				if (status = "true") {
					console.log(this);
					$(this).removeClass("warning").addClass("success");
					$(this).off();
					$(this).text("Opened");
				} else if (status == "false") {
					$(this).addClass("warning");
					
				}
			});

			//search
			var $rows = $('#complaintsTable tr').not('thead tr');
			$('#InputCompTableSearch').keyup(function() {
				var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();

				$rows.show().filter(function() {
					var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
					return !~text.indexOf(val);
				}).hide();
			});

		}
	});

	$("#ComplaintList").slideDown();
}


function openComplaint(cid) {
	console.log("Opening : " + cid);

	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/openComplaint",
		data : {
			'cid' : cid
		},
		success : function(data) {
			return data;
		}
	});
}


function dispdeptNotification() {
	
	$("#dispCont .grid").hide();
	
	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("info").addClass("warning");

	$("#complaintsTable tbody").empty();
	
	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/getdeptNotification",
		success : function(data) {
			// console.log(data);
			$.each(data, function(key, val) {
				var row = "<tr><td>" + val.cid + "</td><td>" + val.location + "</td><td>" + val.date_submission + "</td><td>"
						+ val.description + "</td><td></tr>";
				$("#complaintsTable tbody").append(row);
			});
			
			var $rows = $('#complaintsTable tr').not('thead tr');
			$('#InputCompTableSearch').keyup(function() {
			    var val = $.trim($(this).val()).replace(/ +/g, ' ').toLowerCase();
			    
			    $rows.show().filter(function() {
			        var text = $(this).text().replace(/\s+/g, ' ').toLowerCase();
			        return !~text.indexOf(val);
			    }).hide();
			});

		}
	});

	$("#ComplaintList").slideDown();

	return false;
}
//